add_library(document SHARED)

add_library(nodes SHARED)

option(BUILD_NODES "Whether to build actual nodes (useful to turn off to just run basic tests)" ON)
option(NODES_SCU "Use single compilation unit for nodes (speeds up one time build if you have enough RAM)" OFF)

target_sources(document
    PRIVATE
        context.cpp
        document.cpp
        instance.cpp
        color.cpp
        shading.cpp
        action.cpp
        action_stack.cpp
        node_tree.cpp
        node/abstract_node.cpp
)

set(ALL_NODES
    nodes/frame.cpp
    nodes/animated.cpp
    nodes/frame_list.cpp
    nodes/interpolate.cpp
    nodes/operators.cpp
    nodes/time.cpp
    nodes/time_list.cpp
    nodes/time_period.cpp
    nodes/dynamic_node.cpp
    nodes/morph.cpp
    nodes/format_string.cpp
    nodes/format_number.cpp
    nodes/color.cpp
    nodes/color_mix.cpp
    nodes/follow_path.cpp
    nodes/path_xy.cpp
    nodes/extract_coord.cpp
    nodes/point_xy.cpp
    nodes/knot.cpp
    nodes/knot_list.cpp
    nodes/shading.cpp
    nodes/linear.cpp
    nodes/truncate.cpp
    nodes/to_string.cpp
    nodes/color_string.cpp
    nodes/file_string.cpp
    nodes/average.cpp
    nodes/math.cpp
    nodes/boolean.cpp
    nodes/rectangle.cpp
    nodes/circle.cpp
    nodes/list.cpp
    nodes/compare.cpp
    nodes/conditional.cpp
    nodes/split_string.cpp
    nodes/random.cpp
    nodes/audio.cpp
    nodes/translate.cpp
    nodes/scale.cpp
    nodes/rotate.cpp
    nodes/renderable/empty.cpp
    nodes/renderable/composite.cpp
    nodes/renderable/image.cpp
    nodes/renderable/transform.cpp
    nodes/renderable/text.cpp
    nodes/renderable/render_shape.cpp
)

function(make_single_compile_unit fname file_list)
    set(content "// single compilation unit auto-generated by cmake\n")
    foreach (f ${file_list})
        string(APPEND content "#include \"${CMAKE_CURRENT_LIST_DIR}/${f}\"\n")
    endforeach()
    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${fname} ${content})
endfunction()

if (BUILD_NODES)
    if (NODES_SCU)
        make_single_compile_unit(all_nodes.cpp "${ALL_NODES}")
        target_sources(nodes PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/all_nodes.cpp)
    else()
        target_sources(nodes PRIVATE ${ALL_NODES})
    endif()
endif()

target_link_libraries(document log morphing fmt)
target_link_libraries(nodes document)

install(
    TARGETS document nodes
    EXPORT rainynite_core
    LIBRARY DESTINATION lib/rainynite/
    ARCHIVE DESTINATION lib/rainynite/
)
